// pages/mine/components/mddlookrecord/record.js
const http = require("../../../../utils/http.js")
const utils = require("../../../../utils/util.js")

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    totalPage: 0,

    impressionList: [],


    slideButtons: [{
      type: 'warn',
      extClass: 'test',
      text: '删除'
    }],
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行

    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    initData() {
      http.get("/impression/select", {
          pageIndex: this.data.pageIndex,
          pageSize: this.data.pageSize
        })
        .then(res => {
          this.setData({
            impressionList: res.data.data.resultList,
            totalCount: res.data.data.totalCount,
            totalPage: res.data.data.totalPage
          })
        })
    },
    addData() {
      if (this.data.pageIndex >= this.data.totalPage){
        return;
      }
      this.setData({
        pageIndex: ++this.data.pageIndex
      })
      http.get("/impression/select", {
          pageIndex: this.data.pageIndex,
          pageSize: this.data.pageSize
        })
        .then(res => {
          this.setData({
            impressionList: res.data.data.resultList,
            totalCount: res.data.data.totalCount,
            totalPage: res.data.data.totalPage
          })
        })
    },

    slideButtonTap(e) {
      let self = this
      wx.showModal({
        title: '提示',
        content: '真的要删除这条印象嘛？',
        confirmText: '是的',
        cancelText: '算了',
        success(res) {
          if (res.confirm) {
            http.get("/impression/delete", {
              id: e.currentTarget.dataset.id
            }).then(res => {
              if (res.data.status == 200) {
                self.data.impressionList.splice(e.currentTarget.dataset.index, 1)
                self.setData({
                  impressionList: self.data.impressionList
                })
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 1500
                })
              }
            })
          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
    },

    // ListTouch触摸开始
    ListTouchStart(e) {
      this.setData({
        ListTouchStart: e.touches[0].pageX
      })
    },

    // ListTouch计算方向
    ListTouchMove(e) {
      this.setData({
        ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
      })
    },

    // ListTouch计算滚动
    ListTouchEnd(e) {
      if (this.data.ListTouchDirection == 'left') {
        this.setData({
          modalName: e.currentTarget.dataset.target
        })
      } else {
        this.setData({
          modalName: null
        })
      }
      this.setData({
        ListTouchDirection: null
      })
    },
    gotoImpression(e) {
      let impressionId = e.currentTarget.dataset.id
      wx.navigateTo({
        url: "/pages/impression/detail/detail?id=" + impressionId,
      })
    }
  }
})